// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cfixedframe.h"
#include "ccube.h"

#define MOVE_STEP .4
#define ANGLE_STEP 0.2
#define PI 3.1416f

float giro = 0.1f;
float g_x=0.0f;
float g_y=0.0f;
float g_z=10.0f;
float g_roll= 0.0f;
float g_pitch= 0.0f;
float g_yaw= 0.0f;
int g_w;
int g_h;
float g_cubeAngle= 0.f;

//Variable del cubo
CCube g_ccube;
CCube g_ccube2;

//variables booleanas para saber que tecla se ha pulsado
bool gbkey_w = false;
bool gbkey_s = false;
bool gbkey_a = false;
bool gbkey_d = false;
bool gbkey_f = false;
bool gbkey_l = false;
bool gbkey_g = false;
bool gbkey_k = false;


//variables del mouse
int gdm_x = 0;
int gdm_y = 0;




void Keyboard(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
	/*
	case 'g':
		if(gbkey_g)
		{
			gbkey_g = false;
			Rotar(false);
			printf("GIRAR --> OFF");
		}
		else
		{
			gbkey_g = true;
			printf("GIRAR --> ON");
		}
		break;
	*/
	case 'l':
		if(gbkey_l)
		{
			gbkey_l = false;
			glDisable(GL_LIGHTING);
			glDisable(GL_LIGHT0);
			printf("light --> OFF\n");
		}
		else
		{
			gbkey_l = true;
			glEnable(GL_LIGHTING);
			glEnable(GL_LIGHT0);
			printf("light --> ON\n");
		}
		break;
	case 'f':
		if(gbkey_f)
			gbkey_f = false;
		else
			gbkey_f = true;
		break;
	//culling
	case 'c':
		if(glIsEnabled(GL_CULL_FACE))
		{
			glDisable(GL_CULL_FACE);
			printf("culling disabled\n");
		}
		else
		{
			glEnable(GL_CULL_FACE);
			printf("culling enabled\n");
		}
		break;
	//foward
	case 'w':	
		gbkey_w = true;
		//g_x-= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//backward
	case 's':	
		gbkey_s = true;
		//g_x+= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//right
	case 'd': 
		gbkey_d = true;
		//g_yaw-= ANGLE_STEP; 
		break;
	//left
	case 'a':
		gbkey_a = true;
		//g_yaw+= ANGLE_STEP;
		break;

	case 'k':
		if(gbkey_k)
		{
			gbkey_k = false;
			printf("Alpha --> OFF\n");
		}
		else
		{
			gbkey_k = true;
			printf("Alpha --> ON\n");
		}
		break;
	case 27: exit(0);
	}
}
void KeyboardUp(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
	//foward
	case 'w':	
		gbkey_w = false;
		//g_x-= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//backward
	case 's':	
		gbkey_s = false;
		//g_x+= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//right
	case 'd': 
		gbkey_d = false;
		//g_yaw-= ANGLE_STEP; 
		break;
	//left
	case 'a': //
		gbkey_a = false;
		//g_yaw+= ANGLE_STEP; break;
		break;
	case 27: exit(0);
	}
}
void Set3DView()
{
	GLfloat spot[] = {-1.0,-1.0,-1.0};
	GLfloat position[] = {20.0,20.0,20.0};
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,(GLfloat *)spot);
	glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat *)position);
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glRotatef(-g_roll, 0.0, 0.0, 1.0);	
	glRotatef(-g_yaw, 0.0, 1.0, 0.0);
	glRotatef(-g_pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-g_x, -g_y, -g_z);
}
void DrawScene(void)
{
	//clean the backbuffer
	glClear (GL_COLOR_BUFFER_BIT);

	//viewing transformation
	Set3DView();
	glDisable(GL_DEPTH_TEST);
	//draw the cube
	giro += 0.9;
	glPushMatrix();
	g_ccube.setColor(0.2,1.0,0.2);
	g_ccube.setPosition(0.0,0.5,0.0);
	g_ccube.setScale(0.5, 0.5, 0.5);
	g_ccube.setRotation(giro,1.0,1.0,0.0);
	g_ccube.draw(gbkey_f, gbkey_k);
	glPopMatrix();

	glPushMatrix();
	g_ccube2.setColor(1.0,0.2,0.2);
	g_ccube2.setPosition(2.0,0.0,-3.0);
	g_ccube2.setScale(2.0, 2.0, 2.0);
	g_ccube.setRotation(giro,1.0,0.0,1.0);
	g_ccube2.draw(gbkey_f, gbkey_k);
	glPopMatrix();

	glEnable(GL_BLEND);

	glBegin(GL_QUADS);
	glNormal3f(0, 0, 1);
	glTexCoord2f(0.0,1.0);
	glVertex3f(0, 0, 5);
	glTexCoord2f(1.0,1.0);
	glVertex3f(5, 0, 5);
	glTexCoord2f(1.0,0.0);
	glVertex3f(5, 5, 5);
	glTexCoord2f(0.0,0.0);
	glVertex3f(0, 5, 5);
	glEnd();
	glDisable(GL_BLEND);
}

void Reshape (int w, int h)
{
	//Reshape callback function
	g_h= h;
	g_w= w;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
}

void Mouse (int x, int y) //movimiento del raton
{
	if((x - gdm_x) > 0)
	{
		g_yaw+=ANGLE_STEP;
	}
	if((x - gdm_x) < 0)
	{
		g_yaw-=ANGLE_STEP;
	}
	if((y - gdm_y) > 0)
	{
		g_pitch+=ANGLE_STEP;
	}
	if((y - gdm_y) < 0)
	{
		g_pitch-=ANGLE_STEP;
	}
	// posicion del raton
	gdm_x = x;
	gdm_y = y;
}
int main(int argc, char** argv)
{

	//INIT GLUT/////////////////////
	////////////////////////////////
	//init window and OpenGL context
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize (1024, 768); 
	glutCreateWindow (argv[0]);
//	glutFullScreen();


	//callback functions
	glutDisplayFunc(DrawScene);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);
	glutKeyboardUpFunc(KeyboardUp);
	glutPassiveMotionFunc(Mouse);
	CFixedFrameRate *pFFR;
	pFFR = new CFixedFrameRate(120);
	
	GLfloat difuse[] = {0.5,0.5,0.5,0.1};
	GLfloat ambient[] = {0.1,0.1,0.1,0.1};
	//GLfloat specular[] = {0.1,0.2,0.3,1.0};
	

	glLightfv(GL_LIGHT0,GL_DIFFUSE,(GLfloat *)difuse);
	glLightfv(GL_LIGHT0,GL_AMBIENT,(GLfloat *)ambient);
	//glLightfv(GL_LIGHT0,GL_SPECULAR,(GLfloat *)specular);
	
	glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0);

	g_ccube.setTexture("./textures/textura.jpg");
	g_ccube2.setTexture("./textures/umbrella.png");

	while (1)
	{
		//UPDATE////////////////////
		////////////////////////////
		//"move" the cube
		g_cubeAngle+= 0.1;
		
		//queued events?
		glutMainLoopEvent();

		//MOUSE MOTION//////////////
		////////////////////////////
		

		//KEYSTROKES////////////////
		////////////////////////////
		
		if(gbkey_w)
		{
			g_x-= MOVE_STEP*sin(g_yaw*PI/180);
			g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		}
		if(gbkey_s)
		{
			g_x+= MOVE_STEP*sin(g_yaw*PI/180);
			g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		}
		if(gbkey_d)
		{
			g_yaw+= ANGLE_STEP;
		}
		if(gbkey_a)
		{
			g_yaw-= ANGLE_STEP;
		}



		//RENDER////////////////////
		////////////////////////////
		glutPostRedisplay();
		glutSwapBuffers();

		pFFR->endFrame();
	}
   return 0;
}